package com.neu.config;


import com.neu.service.MinioService;
import io.minio.MinioClient;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.validation.annotation.Validated;


import javax.validation.constraints.NotBlank;

@Data
@Slf4j
@Validated  // 激活配置属性的校验
@Configuration
@ConfigurationProperties(prefix = "minio")
//用于将配置文件中的属性自动映射到Java类的字段上
//从配置文件中读取前缀为minio的配置属性，并将这些属性映射到这个类的字段上。
public class MinioConfig {
    //从配置文件中读取并存储MinIO的相关配置信息
    @NotBlank(message = "MinIO 端点不能为空")
    private String endpoint;
    @NotBlank(message = "MinIO AccessKey 不能为空")
    private String accessKey;
    @NotBlank(message = "MinIO SecretKey 不能为空")
    private String secretKey;
    @NotBlank(message = "MinIO 存储桶名称不能为空")
    private String bucketName;
    @Bean
    public MinioClient minioClient() {
        log.info("初始化MinioClient");
        log.info("endpoint: {}, accessKey: {}, secretKey: {}", endpoint, accessKey, secretKey);
        return MinioClient.builder()
                .endpoint(endpoint)
                .credentials(accessKey, secretKey)
                .build();
    }
    @Bean
    public MinioService minioService(MinioClient minioClient) {
        return new MinioService(minioClient, bucketName);
    }
}